package com.ht.medicaltreatment.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleBoxListAdapter
import com.ht.medicaltreatment.bean.Row2
import com.ht.medicaltreatment.utils.NFCUtils
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.UIDUitls
import com.ht.medicaltreatment.viewmodel.SampleBoxViewModel
import kotlinx.android.synthetic.main.activity_sample_box_layout.etSearch
import kotlinx.android.synthetic.main.activity_sample_box_layout.includeScan
import kotlinx.android.synthetic.main.activity_sample_box_layout.llNFC
import kotlinx.android.synthetic.main.activity_sample_box_layout.recyclerView
import kotlinx.android.synthetic.main.activity_sample_box_layout.refreshLayout
import kotlinx.android.synthetic.main.activity_sample_box_layout.tvCreateSample
import kotlinx.android.synthetic.main.include_scan_layout.btnBack

//样本盒
class SampleBoxActivity:BaseActivity(), View.OnClickListener, OnItemClickListener {

    override fun getLayoutId() = R.layout.activity_sample_box_layout

    override fun isViewStup() = true

    private var isNFC = false
    private lateinit var mModel:SampleBoxViewModel
    private var page = 1
    private lateinit var adapter:SampleBoxListAdapter
    private var isJump = false

    override fun init() {
        setTitle("我的样本盒")

        adapter = SampleBoxListAdapter()
        mModel = ViewModelProvider(this)[SampleBoxViewModel::class.java]
        tvCreateSample.setOnClickListener(this)
        btnBack.setOnClickListener(this)
        llNFC.setOnClickListener(this)
        etSearch.setOnClickListener(this)

        if(Build.MODEL == "V9100"){
            ScanUtils.init()
        }else{
            isNFC = packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
            if(isNFC)NFCUtils(this)
        }

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        refreshLayout.setOnLoadMoreListener {
            page++
            initData()
        }
        initData()
    }

    fun initData(){
        showProgress()
        mModel.sampleBoxList(page,"","",{
            dismissProgress()
            if(page > 1){
                adapter.addData(it)
                refreshLayout.finishLoadMore()
            }else{
                adapter.setNewInstance(it)
            }
        })
    }

    override fun onClick(v: View?) {
        when(v){
            //创建样本盒
            tvCreateSample -> startActivity(BuildSampleBoxActivity::class.java)

            //搜索
            etSearch -> startActivity(SearchSampleBoxActivity::class.java)

            llNFC -> {
                includeScan.visibility = View.VISIBLE
                ScanUtils.open {
                    ScanUtils.close()
                    if(isJump){
                        return@open
                    }
                    isJump = true
                    includeScan.visibility = View.GONE
                    val intent = Intent(this,SearchSampleBoxActivity::class.java)
                    intent.putExtra("uid",it)
                    startActivity(intent)
                }
            }

            btnBack -> includeScan.visibility = View.GONE
        }
    }

    override fun onResume() {
        super.onResume()
        isJump = false
        if(tvCreateSample != null){
            page = 1
            initData()
        }
        if(!isNFC)return
        //开启前台调度系统
        NFCUtils.mNfcAdapter.enableForegroundDispatch(this,NFCUtils.mPendingIntent,NFCUtils.mIntentFilter,
            NFCUtils.mTechList)
    }

    override fun onPause() {
        super.onPause()
        if(!isNFC)return
        //关闭前台调度系统
        NFCUtils.mNfcAdapter.disableForegroundDispatch(this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if(!isNFC)return
        //当该Activity接收到NFC标签时，运行该方法
        //调用工具方法，读取NFC数据
        var uid = NFCUtils.readNFCId(intent)
        uid = UIDUitls.sort(uid)
        includeScan.visibility = View.GONE
        val intent = Intent(this,SearchSampleBoxActivity::class.java)
        intent.putExtra("uid",uid)
        startActivity(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.onDestroy()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as Row2
        val intent = Intent(this,SampleBoxUpdateActivity::class.java)
        intent.putExtra("id",item.id)
        startActivity(intent)
    }
}